package com.zsrd.lessons.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsrd.lessons.domain.TsQuestion;
import com.zsrd.lessons.domain.TsQuestionAnswer;
import com.zsrd.lessons.domain.dto.TsQuestionAddDto;
import com.zsrd.lessons.domain.dto.TsQuestionAnswerAddDto;
import com.zsrd.lessons.domain.dto.TsQuestionUpdateDto;
import com.zsrd.lessons.domain.vo.TsQuestionVo;
import com.zsrd.lessons.mapper.TsQuestionMapper;
import com.zsrd.lessons.service.TsQuestionAnswerService;
import com.zsrd.lessons.service.TsQuestionService;
import com.zsrd.lessons.utils.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 题目表 服务实现类
 * </p>
 *
 * @author Chen Wei
 * @since 2022-07-11
 */
@Service
public class TsQuestionServiceImpl extends ServiceImpl<TsQuestionMapper, TsQuestion> implements TsQuestionService {
    @Autowired
    private TsQuestionAnswerService tsQuestionAnswerService;


    @Override
    public IPage<TsQuestionVo> tsQuestionService(String orgNo, String loginId, String keyword, Long current, Long size) {
        Page page = new Page(current, size);
        IPage<TsQuestionVo> ip = baseMapper.tsQuestionService(page, orgNo, keyword);
        return ip;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addQuestion(TsQuestionAddDto a) {
        List<TsQuestion> list = list(new QueryWrapper<TsQuestion>().eq("title", a.getTitle()).eq("bid", a.getBid()));
        if (!ListUtils.isEmpty(list)) {
            throw new RuntimeException("考试题目重复");
        }
        TsQuestion bean = new TsQuestion();
        BeanUtils.copyProperties(a, bean);
        bean.setUtime(LocalDateTime.now());
        save(bean);
        Integer type = a.getType();
        if (type == 1 || type == 2) {
            List<TsQuestionAnswerAddDto> answerList = a.getAnswerList();
            TsQuestionAnswer tsQuestionAnswer = new TsQuestionAnswer();

            for (int i = 0; i < answerList.size(); i++) {
                TsQuestionAnswerAddDto tsQuestionAnswerAddDto = answerList.get(i);
                tsQuestionAnswer.setId(null);
                tsQuestionAnswer.setUtime(LocalDateTime.now());
                tsQuestionAnswer.setTid(bean.getId());
                BeanUtils.copyProperties(tsQuestionAnswerAddDto, tsQuestionAnswer);
                tsQuestionAnswerService.save(tsQuestionAnswer);
            }
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateQuestion(TsQuestionUpdateDto a) {
        TsQuestion byId = getById(a.getId());
        if (StringUtils.isEmpty(byId.getOrgNo())) {
            throw new RuntimeException("公共题目不允许编辑");
        }

        TsQuestion bean = new TsQuestion();
        BeanUtils.copyProperties(a, bean);
        bean.setUtime(LocalDateTime.now());
        updateById(bean);
        tsQuestionAnswerService.remove(new QueryWrapper<TsQuestionAnswer>().eq("tid", a.getId()));
        Integer type = a.getType();
        if (type == 1 || type == 2) {
            List<TsQuestionAnswerAddDto> answerList = a.getAnswerList();
            TsQuestionAnswer tsQuestionAnswer = new TsQuestionAnswer();

            for (int i = 0; i < answerList.size(); i++) {
                TsQuestionAnswerAddDto tsQuestionAnswerAddDto = answerList.get(i);
                tsQuestionAnswer.setId(null);
                tsQuestionAnswer.setUtime(LocalDateTime.now());
                tsQuestionAnswer.setTid(bean.getId());
                BeanUtils.copyProperties(tsQuestionAnswerAddDto, tsQuestionAnswer);
                tsQuestionAnswerService.save(tsQuestionAnswer);
            }
        }

    }

    @Override
    public void deleteQuestion(Long id) {
        TsQuestion byId = getById(id);
        if (StringUtils.isEmpty(byId.getOrgNo())) {
            throw new RuntimeException("公共题目不允许删除");
        }
        removeById(id);
        tsQuestionAnswerService.remove(new QueryWrapper<TsQuestionAnswer>().eq("tid", id));
    }

    @Override
    public Map<String, Object> lookAnswer(Long id) {
        Map<String, Object> map = new HashMap<>();

        TsQuestion d = getById(id);
        map.put("title", d.getTitle());
        map.put("type", d.getType());
        map.put("analysis", d.getAnalysis());
        if (d.getType() == 3) {
            // 判断题
            map.put("answer", d.getAnswer());
        } else if (d.getType() == 2 || d.getType() == 1) {
            // 多选  单选
            List<TsQuestionAnswer> answers = tsQuestionAnswerService.list(new QueryWrapper<TsQuestionAnswer>().eq("tid", id));
            map.put("answer", answers);
        }
        return map;
    }

    @Override
    public Map<String, Object> questionDetail(Long id) {
        TsQuestion d = getById(id);

        Map<String, Object> map = new HashMap<>();

        map.put("detail", d);
        if (d.getType() == 3) {
            // 判断题
            map.put("answer", d.getAnswer());
        } else if (d.getType() == 2 || d.getType() == 1) {
            // 多选  单选
            List<TsQuestionAnswer> answers = tsQuestionAnswerService.list(new QueryWrapper<TsQuestionAnswer>().eq("tid", id));
            map.put("answer", answers);
        }
        return map;
    }
}
